﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ETL.Common;
using ETL.Common.Logging;
using ETL.Entity;

namespace ETL.DataAccess
{
    public class ProjectScheduledResourceUsageRepository
    {
        public int AddNewScheduledResourceUsage(string genTaskId, string userId, DateTime startDate, DateTime endDate)
        {
            try
            {

            var db = new ETLDataEntities();
            string projectName = string.Empty;
            decimal taskId = 0;
            decimal genResoureId = 0;
            decimal resourceId = 0;

            decimal _genTaskId = decimal.Parse(genTaskId);

            // Get Task by genTaskId
            TASK task = (from t in db.TASKS where t.GEN_TASK_ID == _genTaskId select t).FirstOrDefault();

            if(task != null)
            {
                projectName = task.PROJECT_NAME;
                if(task.TASK_ID != null)
                    taskId = task.TASK_ID.Value;
            }

            // Get resource instance by user id
            RESOURCE_INSTANCES rI =
                (from r in db.RESOURCE_INSTANCES where r.RESOURCE_NAME.ToUpper() == userId.ToUpper() select r).
                    FirstOrDefault();
            if(rI != null)
            {
                genResoureId = rI.GEN_RESOURCE_ID;
                if(rI.RESOURCE_ID != null)
                    resourceId = rI.RESOURCE_ID.Value;
            }

            // Get max scheduled resource id
            decimal maxSchId = 0;
            var schWithMaxSchId =
                (from p in db.SCHEDULED_RESOURCE_USAGES select p).OrderByDescending(p => p.SCHEDULED_RESOURCE_USAGE_ID).FirstOrDefault();

            if (schWithMaxSchId != null)
            {
                if (schWithMaxSchId.SCHEDULED_RESOURCE_USAGE_ID != null)
                    maxSchId = schWithMaxSchId.SCHEDULED_RESOURCE_USAGE_ID.Value;
            }

            // Get max scheduled resource id
            decimal maxGenSchId = 0;
            var schWithMaxGenSchId =
                (from p in db.SCHEDULED_RESOURCE_USAGES select p).OrderByDescending(p => p.GEN_SCH_RESOURCE_USAGE_ID).FirstOrDefault();

            if (schWithMaxGenSchId != null)
            {
                maxGenSchId = schWithMaxGenSchId.GEN_SCH_RESOURCE_USAGE_ID;
            }

            //Add new scheduled resource usage
            var scheduledResourceUsages = new SCHEDULED_RESOURCE_USAGES
                                                                    {
                                                                        SCHEDULED_RESOURCE_USAGE_ID = maxSchId + 1,
                                                                        GEN_SCH_RESOURCE_USAGE_ID = maxGenSchId +1,
                                                                        START_DATE = startDate,
                                                                        END_DATE = endDate,
                                                                        PROJECT_NAME = projectName,
                                                                        TASK_ID = taskId,
                                                                        GEN_TASK_ID = _genTaskId,
                                                                        GEN_RESOURCE_ID = genResoureId,
                                                                        RESOURCE_ID = resourceId,
                                                                        UNITS = 1
                                                                    };
            db.SCHEDULED_RESOURCE_USAGES.Add(scheduledResourceUsages);
            db.SaveChanges();
            return 1;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return -1;
            }
        }

        public int DeleteScheduledResourceUsages(string taskId)
        {
            try
            {

                var db = new ETLDataEntities();
                decimal dTaskId = Utils.StringToDecimal(taskId);

                // Get Task by genTaskId
                TASK task = (from t in db.TASKS where t.TASK_ID == dTaskId select t).FirstOrDefault();


                if (task != null)
                {
                    var scheduledResourceUsages = from s in db.SCHEDULED_RESOURCE_USAGES
                                                  where s.TASK_ID == task.TASK_ID
                                                  select s;
                    if(scheduledResourceUsages != null && scheduledResourceUsages.Count() > 0)
                    {
                        var lstScheduledResourceUsages = scheduledResourceUsages.ToList();
                        foreach (var scheduledResourceUsage in lstScheduledResourceUsages)
                        {
                            db.SCHEDULED_RESOURCE_USAGES.Remove(scheduledResourceUsage);
                        }

                        db.SaveChanges();
                    }

                }

                
                
                
                return 1;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return -1;
            }
        }
    }
}
